<template>
  <el-card shadow="hover">
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="getRoleList">
          搜索
        </el-button>
        <el-button @click="handleReset">清空</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="showAddRole">添加角色</el-button>
    <el-button type="danger" disabled v-permission="'btn.Role.remove'"
      >批量删除</el-button
    >

    <el-table border class="mt-20" :data="roleList" v-loading="loading">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="info"
            size="small"
            :icon="User"
            @click="showRoleAuth(row)"
            v-permission="'btn.Role.assgin'"
          />
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            v-permission="'btn.Role.update'"
          />
          <el-popconfirm
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="Are you sure to delete this?"
            v-permission="'btn.Role.remove'"
          >
            <template #reference>
              <el-button type="danger" size="small" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getRoleList"
      @current-change="getRoleList"
    />

    <el-dialog v-model="visible" title="添加角色">
      <el-form label-width="100px">
        <el-form-item label="角色名称">
          <el-input placeholder="用户名" v-model="roleFormData.roleName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="addRole">保存</el-button>
          <el-button @click="visible = false"> 取消 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XUser",
};
</script>

<script lang="ts" setup>
import {
  Search,
  User,
  Edit,
  Delete,
  InfoFilled,
} from "@element-plus/icons-vue";
import { ref, reactive, onMounted } from "vue";
import type { RoleList, RoleItem } from "@/api/acl/model/userModel";
import { GetRoleListApi, AddRoleApi } from "@/api/acl/role";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

/*************** 角色列表分页展示 ****************/
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const loading = ref(false);

const roleName = ref("");
const roleList = ref<RoleList>([]);

const getRoleList = async () => {
  loading.value = true;
  const res = await GetRoleListApi({
    page: currentPage.value,
    limit: pageSize.value,
    roleName: roleName.value,
  });
  total.value = res.total;
  roleList.value = res.items;
  loading.value = false;
};

onMounted(getRoleList);

// const handleSizeChange = (val: number) => {
//   console.log(`${val} items per page`);
// };
// const handleCurrentChange = (val: number) => {
//   console.log(`current page: ${val}`);
// };

// 按下清空按钮
const handleReset = () => {
  // 清空角色名称输入框
  roleName.value = "";
  // 重新发起请求
  getRoleList();
};

/*************** 新增角色 ****************/
const visible = ref(false);

const roleFormData = reactive({
  roleName: "",
});

// 点击保存按钮
const addRole = async () => {
  await AddRoleApi(roleFormData.roleName);
  // 更新页面数据
  getRoleList();
  ElMessage.success("新增角色成功！");
  visible.value = false;
};

// 点击添加角色按钮
const showAddRole = () => {
  visible.value = true;
};

/*************** 跳转展示角色权限列表 ****************/
const router = useRouter();
const showRoleAuth = (row: RoleItem) => {
  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
</script>

<style scoped></style>
